#include "tools.h"

#include <QByteArray>
#include <QString>
#include <QTextCodec>

QString GBK2UTF8(QByteArray inStr)
{
    auto gbk = QTextCodec::codecForName("gbk");
    auto utf8 = QTextCodec::codecForName("UTF-8");
    char* p = inStr.data();
    QString str = gbk->toUnicode(p);
    QByteArray utf8_bytes = utf8->fromUnicode(str);
    p = utf8_bytes.data();
    str = p;
    return str;
}

QString UTF82GBK(QByteArray inStr)
{
    auto gbk = QTextCodec::codecForName("gbk");
    auto utf8 = QTextCodec::codecForName("UTF-8");
    char* p = inStr.data();
    QString str = utf8->toUnicode(p);
    QByteArray utf8_bytes = gbk->fromUnicode(str);
    p = utf8_bytes.data();
    str = p;
    return str;
}

unsigned char check(unsigned char dat[], int n)
{
    unsigned char checkNum = 0;
    for (int i = 0; i < n; ++i)
    {
        checkNum ^= dat[i];
    }
    return checkNum;
}
